﻿{include file="public/header"/}
</head>
<body>
<style>
.layui-table-cell {height:30px!important;}
@media screen and (max-width: 500px) {
.layui-layer.layui-layer-page{width:100% !important;overflow:hidden !important;left:0 !important;}
.layui-layer-title{width:100% !important;box-sizing:border-box;}
.layui-layer-content{width:100% !important;}
 }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body table-tool-mini">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">角色名：</label>
                        <div class="layui-input-inline mr0">
                            <input name="name" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchRole" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="admin_add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                    </div>
                </div>
            </div>
            <table class="layui-table" id="tableRole" lay-filter="tableRole"></table>
        </div>
    </div>
</div>
<script type="text/html" id="roleTbBar">
<a class="layui-btn layui-btn-primary layui-btn-sm" lay-event="admin_edit" data-id="{{d.id}}">编辑</a>
<a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="admin_del" data-id="{{d.id}}">删除</a>
</script>
{include file="public/footer"/}
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'zTree'], function () {
            var $ = layui.jquery;
            var layer = layui.layer;
            var form = layui.form;
            var table = layui.table;
            var util = layui.util;
            var admin = layui.admin;
            // 渲染表格
            var insTb = table.render({
                elem: '#tableRole',
                url: "{:url('/system.authrole/index')}",
                page: true,
                cellMinWidth: 100,
                //height : 'full-200',
                cols: [[
                    {field:'id',title:'ID',width:60},
                    {field: 'name',title: '角色名',width:180},
                    {field: 'online_update', title: '在线升级',align: 'center',templet: function (d) {
                        if (d.online_update ==1) {
                            return '是';
                        } else {
                            return '否';
                        }
                    },width:180},
                    {field: 'only_oneself', title: '文档权限',align: 'center',templet: function (d) {
                        if (d.only_oneself ==1) {
                           return '只能看自己发布';
                        } else {
                           return '查看全部';
                        }
                    },width:180},
                    {field: 'remark', title: '备注'},
                    {field: 'add_time', title: '创建时间', width: 200,align: 'center',},
                    {toolbar: '#roleTbBar', title: '操作',align: 'center', width: 200}
                ]]
            });
            // 搜索
            form.on('submit(formSubSearchRole)', function (data) {
                insTb.reload({where: data.field}, 'data');
            });
            // 添加
            $('#admin_add').click(function () {
                showPermModel();
            });            
            // 表格工具条点击事件
            table.on('tool(tableRole)', function (obj) {
                if (obj.event === 'admin_edit') { // 修改
                    showPermModel(obj.data.id);
                } else if (obj.event === 'admin_del') { // 删除
                    var del_id = $(this).data('id');
                    layer.msg('确定要删除“' + obj.data.name + '”角色吗？', {
                        btnAlign: 'c',
                        time: 0,
                        btn: ['确定', '取消'],
                        yes: function(index, layero){
                            layer.close(index);
                            var loadIndex = layer.load(2);
                            $.post("{:url('/system.authrole/del')}", {id: del_id}, function (res) {
                                layer.close(loadIndex);
                                if (res.code === 0) {
                                    layer.msg(res.msg, {icon: 1, time: 1000});
                                    insTb.reload({page: {curr: 1}});
                                } else {
                                    layer.msg(res.msg, {icon: 2, time: 1000});
                                }
                            }, 'json');
                        }
                    });
                }
            });
            // 权限管理
            function showPermModel(roleid) {
                if(roleid){
                    layer.open({
                        type: 2,
                        title: '修改权限',
                        fixed: true,
                        shadeClose: false,
                        shade: 0.3,
                        area: ['100%', '100%'],
                        content: "{:url('/system.authrole/edit')}?id="+roleid
                    });                   
                }else{
                    layer.open({
                        type: 2,
                        title: '添加权限',
                        fixed: true,
                        shadeClose: false,
                        shade: 0.3,
                        area: ['100%', '100%'],
                        content: "{:url('/system.authrole/add')}"
                    });                      
                }
            }
    });
</script>
</body>
</html>